package org.light.shiroauth.verb;

import java.util.ArrayList;
import java.util.List;

import org.javaforever.gatescore.utils.StringUtil;
import org.light.complexverb.ListMyActive;
import org.light.core.Verb;
import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.JavascriptBlock;
import org.light.domain.JavascriptMethod;
import org.light.domain.Method;
import org.light.domain.Signature;
import org.light.domain.Statement;
import org.light.domain.Type;
import org.light.easyui.EasyUIPositions;
import org.light.exception.ValidateException;
import org.light.utils.WriteableUtil;

public class ReadMySession extends Verb implements EasyUIPositions {
	protected Domain roleDomain;
	protected Domain privilegeDomain;
	
	public ReadMySession(Domain userDomain,Domain roleDomain, Domain privilegeDomain) {
		super();
		this.domain = userDomain;
		this.roleDomain = roleDomain;
		this.privilegeDomain = privilegeDomain;
		this.verbName = "ReadMySession";
	}

	@Override
	public Method generateDaoImplMethod() throws Exception {
		return null;
	}	

	@Override
	public Method generateDaoMethodDefinition() throws Exception {
		return null;		
	}

	@Override
	public String generateDaoImplMethodString() throws Exception {
		return null;
	}

	@Override
	public String generateDaoMethodDefinitionString() throws Exception {	
		return null;
	}

	@Override
	public String generateDaoImplMethodStringWithSerial() throws Exception {
		return null;
	}

	@Override
	public Method generateServiceMethodDefinition() throws Exception {
		return null;
	}

	@Override
	public String generateServiceMethodDefinitionString() throws Exception {
		return null;
	}

	@Override
	public Method generateServiceImplMethod() throws Exception  {
		return null;
	}

	@Override
	public String generateServiceImplMethodString() throws Exception {
		return null;
	}

	@Override
	public String generateServiceImplMethodStringWithSerial() throws Exception  {
		return null;
	}
	
	public ReadMySession(){
		super();
		this.setLabel("读出会话");
	}
	
	public ReadMySession(Domain domain) throws ValidateException{
		super();
		this.domain = domain;
		this.denied = domain.isVerbDenied("ReadMySession");
		this.setVerbName("ReadMySession");
		this.setLabel("读出会话");
		if  (domain.getLanguage().equalsIgnoreCase("english"))  this.setLabel("ReadMySession");
	}

	@Override
	public Method generateControllerMethod() throws Exception {
		if (this.denied) return null;
		else {
			Method method = new Method();
			method.setStandardName("readMySession");
			method.setReturnType(new Type("Map<String,Object>"));
			method.setThrowException(true);
			method.addAdditionalImport(this.domain.getPackageToken()+ "."+this.domain.getDomainSuffix()+"."+this.domain.getCapFirstDomainNameWithSuffix());
			method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getServiceSuffix()+"."+this.domain.getStandardName()+"Service");
			method.addAdditionalImport(this.domain.getPackageToken()+".utils.StringUtil");
			method.addAdditionalImport(this.domain.getPackageToken()+ "."+this.roleDomain.getDomainSuffix()+"."+this.roleDomain.getCapFirstDomainNameWithSuffix());
			method.addAdditionalImport(this.domain.getPackageToken()+"."+this.roleDomain.getServiceSuffix()+"."+this.roleDomain.getStandardName()+"Service");
			method.addAdditionalImport(this.domain.getPackageToken()+ "."+this.privilegeDomain.getDomainSuffix()+"."+this.privilegeDomain.getCapFirstDomainNameWithSuffix());
			method.addAdditionalImport("org.apache.shiro.session.Session");
			method.addAdditionalImport("java.util.Set");
			method.addAdditionalImport("java.util.HashSet");
			method.addAdditionalImport("java.util.ArrayList");
			method.addSignature(new Signature(1,"httpSession","HttpSession"));
			method.addMetaData("RequestMapping(value = \"/readMySession\", method = RequestMethod.GET)");
	
			List<Writeable> sList = new ArrayList<Writeable>();
			sList.add(new Statement(1000L,2,"Map<String,Object> result = new TreeMap<String,Object>();"));
			sList.add(new Statement(2000L,2,"Session session  = SecurityUtils.getSubject().getSession();"));
			sList.add(new Statement(3000L,2,""+this.domain.getCapFirstDomainNameWithSuffix()+" "+this.domain.getLowerFirstDomainName()+" = ("+this.domain.getCapFirstDomainNameWithSuffix()+")session.getAttribute(\"user\");"));
			sList.add(new Statement(4000L,2,"if("+this.domain.getLowerFirstDomainName()+"!=null&&!StringUtil.isBlank("+this.domain.getLowerFirstDomainName()+".get"+this.domain.getDomainName().getCapFirstFieldName()+"())) {"));
			sList.add(new Statement(5000L,3,""+this.domain.getLowerFirstDomainName()+" = service.find"+this.domain.getCapFirstDomainName()+"By"+this.domain.getDomainName().getCapFirstFieldName()+"("+this.domain.getLowerFirstDomainName()+".get"+this.domain.getDomainName().getCapFirstFieldName()+"());"));
			sList.add(new Statement(6000L,0,""));
			sList.add(new Statement(7000L,2,""));
			sList.add(new Statement(8000L,3,"Set<String> roleNames=new HashSet<String>();"));
			sList.add(new Statement(9000L,3,"List<String> privilegeNameList=new ArrayList<String>();"));
			sList.add(new Statement(10000L,3,""));
			
			ListMyActive lma = new ListMyActive(this.domain,this.roleDomain);
			ListMyActive rlma = new ListMyActive(this.roleDomain,this.privilegeDomain);
			sList.add(new Statement(11000L,3,"Set<"+this.roleDomain.getCapFirstDomainNameWithSuffix()+"> roles = service."+StringUtil.lowerFirst(lma.getVerbName())+"("+this.domain.getLowerFirstDomainName()+".get"+this.domain.getDomainId().getCapFirstFieldName()+"());"));
			sList.add(new Statement(12000L,3,"for("+this.roleDomain.getCapFirstDomainNameWithSuffix()+" role:roles){"));
			sList.add(new Statement(13000L,4,"roleNames.add(role.get"+this.roleDomain.getDomainName().getCapFirstFieldName()+"());"));
			sList.add(new Statement(14000L,4,"Set<"+this.privilegeDomain.getCapFirstDomainNameWithSuffix()+"> privileges = "+this.roleDomain.getLowerFirstDomainName()+"Service."+StringUtil.lowerFirst(rlma.getVerbName())+"(role.get"+this.roleDomain.getDomainId().getCapFirstFieldName()+"());"));
			sList.add(new Statement(15000L,4,"for ("+this.privilegeDomain.getCapFirstDomainNameWithSuffix()+" privilege: privileges) {"));
			sList.add(new Statement(16000L,5,"privilegeNameList.add(privilege.get"+this.privilegeDomain.getDomainName().getCapFirstFieldName()+"());"));
			sList.add(new Statement(17000L,4,"}"));
			sList.add(new Statement(18000L,3,"}"));
			sList.add(new Statement(19000L,1,""));
			sList.add(new Statement(20000L,3,"if(roleNames==null || roleNames.isEmpty())"));
			sList.add(new Statement(21000L,3,"{"));
			sList.add(new Statement(22000L,4,"roleNames.add(\"noAnyAuth\");"));
			sList.add(new Statement(23000L,3,"}"));
			sList.add(new Statement(24000L,2,""));
			sList.add(new Statement(25000L,3,"result.put(\"success\",true);"));
			sList.add(new Statement(26000L,3,"result.put(\"user\","+this.domain.getLowerFirstDomainName()+");"));
			sList.add(new Statement(27000L,3,"result.put(\"roles\", roleNames);"));
			sList.add(new Statement(28000L,3,"result.put(\"perms\", privilegeNameList);"));
			sList.add(new Statement(29000L,3,"return result;"));
			
			sList.add(new Statement(30000L,2,"}else {"));
			sList.add(new Statement(31000L,3,"result.put(\"success\",false);"));
			sList.add(new Statement(32000L,3,"result.put(\"user\",null);"));
			sList.add(new Statement(33000L,3,"result.put(\"roles\", null);"));
			sList.add(new Statement(34000L,3,"result.put(\"perms\", null);"));
			sList.add(new Statement(35000L,3,"return result;"));
			sList.add(new Statement(36000L,2,"}"));

			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;
		}
	}	

	@Override
	public String generateControllerMethodString()throws Exception {
		if (this.denied) return null;
		else {
			Method m = this.generateControllerMethod();
			return m.generateMethodString();
		}
	}

	@Override
	public String generateControllerMethodStringWithSerial() throws Exception {
		if (this.denied) return null;
		else {
			Method m = this.generateControllerMethod();
			m.setContent(m.generateMethodContentStringWithSerial());
			m.setMethodStatementList(null);
			return m.generateMethodString();
		}
	}

	@Override
	public JavascriptBlock generateEasyUIJSButtonBlock() throws Exception {
		return null;	
	}

	@Override
	public String generateEasyUIJSButtonBlockString() throws Exception {
		return null;
	}



	@Override
	public String generateEasyUIJSButtonBlockStringWithSerial() throws Exception {
		 return null;
	}

	@Override
	public JavascriptMethod generateEasyUIJSActionMethod() throws Exception {
		return null;		
	}

	@Override
	public String generateEasyUIJSActionString() throws Exception {
		return null;
	}

	@Override
	public String generateEasyUIJSActionStringWithSerial() throws Exception {
		 return null;
	}

	public Domain getRoleDomain() {
		return roleDomain;
	}

	public void setRoleDomain(Domain roleDomain) {
		this.roleDomain = roleDomain;
	}

	public Domain getPrivilegeDomain() {
		return privilegeDomain;
	}

	public void setPrivilegeDomain(Domain privilegeDomain) {
		this.privilegeDomain = privilegeDomain;
	}
}
